<!DOCTYPE HTML>
<?php 
	session_start(); 
	
	if(isset($_SESSION['id'], $_SESSION['vorname'], $_SESSION['name'])){
		$id = $_SESSION['id'];
		$vorname = $_SESSION['vorname'];
		$name = $_SESSION['name'];
	}

?>
<html>

<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<link href='./images/favicon.ico' rel='shortcut icon' type='image/png'>
<link rel="stylesheet" type="text/css" href="./css/index.css">
<title>Staff</title>

<script>
function Guarda(id) {
	var formulario = "formulario"+id;
	document.formulario.submit();
}
function Rango(id) {
	var formulario = "rank"+id;
	document.formulario.submit();
}

</script>
</head>


<body id="fondo">
<div id="cuerpo">
<fieldset style="width: 1000px; color: #FFF;";>
    <?php
        include 'header.php';
    ?>
<div id="subcuerpo">
	<table id='staff_list'>
		<tr><td><h2>ID</h2></td><td><h2>Vorname</h2></td><td><h2>Name</h2></td>
			<td><h2>Birth</h2></td><td><h2>Admis</h2></td><td><h2>Gender</h2></td>
			<td><h2>Rank</h2></td><td><h2>Loc.</h2></td>
			<td><h2>Salary</h2></td><td><h2>Image</h2></td></tr>
		<tr><td><h2></h2></td><td><h2></h2></td><td><h2></h2></td>
			<td><h2></h2></td><td colspan=3>Male / Female</td><td><h2></h2></td>
			<td><h2></h2></td><td><h2></h2></td></tr>

<?php
$link = mysqli_connect('localhost', 'root', '');

if (mysqli_connect_errno()) {
        echo 'Cannot connect to mysql';
        exit;
}

$sql = "USE web_db";
$resultado = mysqli_query($link,$sql);
$sql = "SELECT id,vorname,name,birth_date,admission_date,gender,workstation,location,salary,image FROM staff
	WHERE id<>'root'";
$resultado = mysqli_query($link,$sql);
if (!$resultado) {
    echo "Error DB, data cannot be listed\n";
    printf("Errormessage: %s\n", $mysqli->error);
    exit;
}
//Este bucle te dice cuántos resultados se han obtenido para la tabla escogida
while ($fila = mysqli_fetch_row($resultado)) {
    echo "<tr>";
        echo "<td>";
        echo "<input class='staff_el' type='text' value='$fila[0]'></input>";
        echo "</td>";
        echo "<td>";
        echo "<input class='staff_el' type='text' value='$fila[1]'></input>";
        echo "</td>";
        echo "<td>";
        echo "<input class='staff_el' type='text' value='$fila[2]'></input>";
        echo "</td>";
        echo "<td>";
        echo "<input type='date' class='staff_el' value='$fila[3]'></input>";
        echo "</td>";
        echo "<td>";
        echo "<input type='date' class='staff_el' value='$fila[4]'></input>";
        echo "</td>";
        echo "<td>";
        	echo "<input type='radio' value='M'";if ($fila[5]=="M") echo "checked='true'";
        	echo "name='gender".$fila[0]."'></input>";
        	echo "<input type='radio' value='F'";if ($fila[5]=="F") echo "checked='true'"; 
        	echo "name='gender".$fila[0]."'></input>";
        echo "</td>";
        echo "<td>";
        echo "<form name='rank$fila[0]' method=POST action='rank_mod.php'>";
        echo "<input type='hidden' name='mod_id' value='$fila[0]' />";
        echo "<input onchange=Rango('$fila[0]') name='rank' class='staff_el' type='text' value='$fila[6]'></input>";
        echo "</form>";
        echo "</td>";
        echo "<td>";
        echo "<input class='staff_el' type='text' value='$fila[7]'></input>";
        echo "</td>";
        echo "<td>";
        echo "<form name='formulario$fila[0]' method=POST action='sal_mod.php'>";
        echo "<input type='hidden' name='mod_id' value='$fila[0]' />";
        echo "<input onchange=Guarda('$fila[0]') name='sal' class='staff_el' type='text' value='$fila[8]'></input>";
        echo "</form>";
        echo "</td>";
        echo "<td>";
        echo "<img width='120' height='120' src='$fila[9]'></img>";
        echo "</td>";

    echo "<td><form name='borrar' method='POST' action='erase_staff.php'>".
        "<input type='hidden' name='erase_id' value='$fila[0]' />".
        "<input type='submit' value='Delete'/> </form></td>";
    //Fila completada
    echo "</tr>";
}

?>		
	</table>
<p></p>
    <table>
        <tr>
            <td>Total payd per month:</td>
            <td>
                <?php 
                    Salary();
                ?>
            </td>
        </tr>
        <tr>
            <td>Highest salary:</td>
            <td>
                <?php 
                    Highest();
                ?>
            </td>
        </tr>
    </table>
</div>

	<?php
		include 'footer.php';
	?>
</fieldset>
</div>
</body>

<?php 
function Salary(){
    $sql = "USE web_db";
    $link = mysqli_connect('localhost', 'root', '');
    $result = mysqli_query($link,$sql);
    
    $sql = "SELECT SUM(salary) 'Total/Mes'
            FROM staff";
    $result = mysqli_query($link,$sql);
    $row = mysqli_fetch_row($result);

    echo $row[0];
}

function Highest(){
    $sql = "USE web_db";
    $link = mysqli_connect('localhost', 'root', '');
    $result = mysqli_query($link,$sql);
    
    $sql = "SELECT salary, name
            FROM staff
            WHERE salary=(  SELECT MAX(salary)
                            FROM staff)";
    $result = mysqli_query($link,$sql);
    $row = mysqli_fetch_row($result);

    echo "$row[0]€ - $row[1]";
}

?>
